require(['validator'], function () {
    var $right = $("#alarmSettingArticle"),
        corpId = "no";
    var $sysSettingForm = $('#sysSettingForm');
    $sysSettingForm.initForm();
    // 左侧树
    commonFun.leftTreeNodeClickObj['sysAlarmCorpIdSet'] = function (event, treeId, treeNode) {
        corpId = treeNode.corpId;
        if (treeNode.level == 0) {
            corpId = "";
        }
        $("#corpId").val(corpId);
        initRight();
    };

    $(window).on('resize', function () {
    	$right.height($(window).height() - 67);
    }).trigger('resize');

    var initRight = function () {
    	for(var i=1;i<16;i++){
    		$("#drvBhv"+i).prop("checked", false);
    	}
    	var node = commonFun.getLeftTreeSelectedNodes();
    	if(node && node.length > 0){
    		corpId = node[0].corpId;
    		$.ajaxExt({
    			async: false,
    			url: "/alarmPushSetting/searchAlarmUnsafeSetting.htm",
    			data: {"corpId": corpId}
    		}).done(function (data) {
    			if (data) {
    				console.info(data);
    				if(data[0]){
    					for(var i=1;i<16;i++){
    						var json = data[0];
    						if(json.hasOwnProperty("drvBhv"+i) && json["drvBhv"+i]==1) $("#drvBhv"+i).prop("checked", true);
    						
    					}
    				}
    			} 
    		});
    	}
    	
    };

    $("#saveSetting").on("click", function () { // 保存
    	if(corpId=='no' || corpId == ''){
    		$.messager.alert("提示", "请选择左侧树企业。", "info");
    		return;
    	}
//        var result = $.validator($sysSettingForm);
        var data = {};
        data.corpId = corpId;
        for(var i=1;i<16;i++){
        	if($("#drvBhv"+i).is(':checked'))
        		data["drvBhv"+i] = $("#drvBhv"+i).val();
        	else
        		data["drvBhv"+i] = "0";
        }
//        if (result) {
        console.info(data);
        	$.ajaxExt({
                url: "/alarmPushSetting/setAlarmUnsafe.htm",
                data: data//$sysCodeDscForm.serializeArray()
            }).done(function (data) {
                initRight();
                $.messager.alert("提示", "保存成功。", "info");
            });
//        }
    });
    $("#resetSetting").on("click", function () { // 重置
        initRight();
    });
    initRight();
});